home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / QSORT.C < prev    next >
Text File  |  1986-05-18  |  2KB  |  67 lines

  1. /* 1.0  11-27-84 */
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1984        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10.  
  11. LOCAL char *_base;
  12. LOCAL unsigned _width;
  13. LOCAL int (*_compare)();
  14.  
  15. /************************************************************************/
  16.     VOID
  17. qsort(b, n, w, comp)    /* Quicksort n elements beginning at base b, of
  18.                width w characters each, using comparison
  19.                function comp(p, q), which is greater than 0
  20.                if, and only if, elements *p and *q are
  21.                out of sort.                    */
  22. /*----------------------------------------------------------------------*/
  23. char *b;
  24. unsigned n, w;
  25. int (*comp)();
  26. {
  27.     VOID _swap();
  28.     int _comp();
  29.  
  30.     _base = b;
  31.     _width = w;
  32.     _compare = comp;
  33.     sortQ(n, _comp, _swap);
  34. }
  35.  
  36. /************************************************************************/
  37.     LOCAL VOID
  38. _swap(i, j)        /* exchange elements i and j of base array.    */
  39.  
  40. /*----------------------------------------------------------------------*/
  41. unsigned i, j;
  42. {
  43.     char *I, *J, c;
  44.     unsigned k;
  45.  
  46.     I = _base + _width * i;
  47.     J = _base + _width * j;
  48.     for (k = 0; k < _width; k++)
  49.     {    c = *I;
  50.         *I++ = *J;
  51.         *J++ = c;
  52.     }
  53. }
  54. /*\p**********************************************************************/
  55.     LOCAL
  56. _comp(i, j)    /* convert comparison access from index to pointer.    */
  57.  
  58. /*----------------------------------------------------------------------*/
  59. unsigned i, j;
  60. {
  61.     char *I, *J;
  62.  
  63.     I = _base + _width * i;
  64.     J = _base + _width * j;
  65.     return ((*_compare)(I, J));
  66. }
  67.